package com.app.signing.view.setting.export

import android.os.Environment
import com.app.signing.app.App
import com.app.signing.data.DbManager
import java.io.File

object ExportExcel {

    private val title = arrayOf("序号", "学号", "姓名", "学院", "区队", "学习时长(分)")

    fun export(name: String) {
        val dir = File(
                Environment.getExternalStorageDirectory().absolutePath + File.separator + "SigningRecord"
        )
        makeDir(dir)
        val fileName = dir.absolutePath + File.separator + "$name.xls"
        ExcelUtils.initExcel(fileName, title)
        ExcelUtils.writeObjListToExcel(buildData(), fileName, App.app)
    }

    private fun buildData(): List<List<String>> {
        val recordList = ArrayList<ArrayList<String>>()
        val signingData = DbManager.INSTANCE.loadSigningData()
        signingData.forEach {
            val list = ArrayList<String>()
            list.add(it.id.toString())
            list.add(it.userId)
            list.add(it.userName)
            list.add(it.userFaculty)
            list.add(it.userTeam)
            list.add(it.studyMin.toString())
            recordList.add(list)
        }
        return recordList
    }

    private fun makeDir(dir: File) {
        if (!dir.parentFile.exists()) {
            makeDir(dir.parentFile)
        }
        dir.mkdir()
    }
}